<!--
*@Author: QWP
*@Description: 人员调动
*@Date: 2024-07-08 17:04:38
-->
<template>
  <u-dialog v-bind="$attrs"
    :confirmLoading="formLoading"
    destroyOnClose
    @submit="submit"
    @close="dialogClose">
    <a-layout class="f-h-100">
      <a-layout-content class="s-bg-white"
        style="overflow: auto">
        <u-busi-card title=""
          :bordered="false">
          <u-form :elements="formEls"
            :model="form"
            :gutter="16"
            :loading="formLoading"
            @formChange="getUser"
            ref="formRef">
          </u-form>
        </u-busi-card>
      </a-layout-content>
    </a-layout>
  </u-dialog>
</template>

<script lang='ts' setup>
import { ref, reactive, computed, onMounted, h, useAttrs, watch } from 'vue' 
import { useLoginStore,  useBaseStore } from '@/store'
import { storeToRefs } from 'pinia'
import { useRoute, useRouter } from 'vue-router'
import { _IFormEl, _IExtra } from '@/src/components/common/form/interface';
import { FormInstance, FormItemProps } from 'ant-design-vue';
import { HomeOutlined, FileTextOutlined, UploadOutlined } from '@ant-design/icons-vue';
import { TreeProps } from 'ant-design-vue/es/vc-tree';
import { ColumnProps } from 'ant-design-vue/es/table';
import { _IColumnProps } from '@/components/common/tableEdit/interface.ts'
import { fileCols } from '@/common/model/fileCols'
import { cloneDeep } from 'lodash'
import { educations, sexs} from '@/common/model/base'
import { alertC, messageC, numberC, isValue } from '@/util/common'
import { useSetViewPath } from '@/hooks/useFile'
import { insureFormEls, insureRecordCols } from './data/data.ts'
import { getCurrentApi } from '@/views/oa/personnelManager/personInfo/data/api.ts'

interface Props {
  userInfo: object,
}
const props = defineProps<Props>()
const route = useRoute()
const attrs = useAttrs()
const { userInfo } = storeToRefs(useLoginStore())
const { deptData } = storeToRefs(useBaseStore())

const emits = defineEmits(['success'])

// 表单相关信息
const formRef = ref<FormInstance>()
const form = ref({})
const formLoading = ref<boolean>(false)
const formItemProps: FormItemProps & _IExtra = {
  labelWidth: '80px',
  colon: false
}
const formEls: Array<_IFormEl> = reactive([
  { label: '人员姓名', key: 'perName', col: { span: 24,}, element: 'input', 
    formItemProps: { ...formItemProps, required: true },
    componentProps: { disabled: true },
  },
  { label: '原部门', key: 'perSeDeptId', col: { span: 24,}, element: 'select', 
    formItemProps: { ...formItemProps, required: true },
    componentProps: {
      options: deptData.value, disabled: true
    }
  },
  { label: '生效日期', key: 'takeEffectDate', col: { span: 24,}, element: 'datepicker', 
    formItemProps: { ...formItemProps, required: true },
  },
  { label: '调动后信息', key: '', col: { span: 24,}, element: 'text', 
    formItemProps: { ...formItemProps, required: false, colon: false },
  },
  { label: '部门', key: 'perDesDeptId', col: { span: 24,}, element: 'select', 
    formItemProps: { ...formItemProps, required: true },
    componentProps: {
      options: deptData.value
    }
  },
])

// 确认提交
const submit = async () => {
  try {
    await formRef.value.form.validate()
  } catch (error) {
    messageC("请现将表单信息填写完毕！");
    return
  }

  const api = getCurrentApi('transfer');
  if(!api) {
    return
  }

  formLoading.value = true;
  try {
    const res = await api.add([form.value])
    if(res.code == 0) {
      emits('success');
      messageC(res.msg, 'success')
    }
    else {
      messageC(res.msg, 'warning')
    }
  } catch (error) {}
  formLoading.value = false;
}

// 初始化默认保存传递的数据
const initBaseInfo = () => {
  form.value = {
    perInfoNoId: props.userInfo["keyid"] || '',
    perName: props.userInfo["personnelName"] || '',
    perSeDeptId: props.userInfo["departmentId"] || '',
  }
}

// 弹窗关闭
const dialogClose = () => {
  formRef.value.form.resetFields()
  form.value = {}
}

watch(() => attrs.open, async (nval, oval) => {
  if (nval == true) {
    initBaseInfo()
  }
})
</script>

<style lang='scss' >
</style>